﻿using System.Collections.Generic;
using System.Data.Entity.Migrations;
using Pigg.Model;

namespace Pigg.Data
{
    public static class InitDbContext
    {
        public static void Seed(PiggDbContext context)
        {
            var cMenuItem = new AuthPermission {Name = "Create menu items"};
            var uMenuItem = new AuthPermission {Name = "Update menu items"};
            var dMenuItem = new AuthPermission {Name = "Delete menu items"};

            var cPage = new AuthPermission { Name = "Create pages" };
            var uPage = new AuthPermission { Name = "Update pages" };
            var dPage = new AuthPermission { Name = "Delete pages" };

            var permissions = new List<AuthPermission> {cMenuItem, uMenuItem, dMenuItem, cPage, uPage, dPage};

            permissions.ForEach(a => context.AuthPermissions.AddOrUpdate(b => b.Name, a));

            var adminRole = new AuthRole
            {
                Name = "Admin",
                AuthPermissions = permissions
            };

            var writerRole = new AuthRole
            {
                Name = "Writer",
                AuthPermissions = new List<AuthPermission>{ cPage, uPage, dPage }
            };
            context.AuthRoles.AddOrUpdate(r => r.Name, adminRole, writerRole);

            var person = new Person {Email = "admin@admin.com", AuthRoles = new List<AuthRole> {adminRole}};
            context.People.AddOrUpdate(p => p.Email, person);

            var homePage = new ContentPage
            {
                CultureCode = "en",
                Description = "Welcome to this new website",
                Title = "Welcome",
                Url = "/pages/home"
            };
            context.ContentPages.AddOrUpdate(h => h.Title, homePage);

            var nameField = new ContentItem { Key = "name", ItemType = "text", Value = "Type your name" };
            var submitButton = new ContentItem { Key = "submit", ItemType = "button" };
            var cancelButton = new ContentItem { Key = "cancel", ItemType = "button" };
            var contactPage = new ContentPage
            {
                CultureCode = "en",
                Description = "Fill the form to contact us",
                Title = "Contact us",
                Url = "/pages/contactus",
                ContentItems = new List<ContentItem> { nameField, submitButton, cancelButton }
            };
            context.ContentPages.AddOrUpdate(h => h.Title, contactPage);

            // Main Menu
            var mainMenu = new MenuContainer {CultureCode = "en", Name = "Main", Order = 0};
            context.MenuContainers.AddOrUpdate(h => h.Name, mainMenu);
            var home = new MenuItem
            {
                Order = 0,
                Title = "Home Page",
                Content = "<i class='icon-dashboard'></i> HomePage",
                Url = "/pages/home",
                TemplateUrl = "app/page/page.html",
                MenuContainer = mainMenu
            };
            var contactus = new MenuItem
            {
                Order = 1,
                Title = "Contact Us",
                Content = "<i class='icon-dashboard'></i> Contact us",
                Url = "/pages/contactus",
                TemplateUrl = "app/page/page.html",
                MenuContainer = mainMenu
            };
            context.MenuItems.AddOrUpdate(h => h.Title, home);
            context.MenuItems.AddOrUpdate(h => h.Title, contactus);

            // SideNav menu
            var sideNav = new MenuContainer { CultureCode = "en", Name = "SideNav", Order = 0 };
            context.MenuContainers.AddOrUpdate(h => h.Name, mainMenu);
            var pages = new MenuItem
            {
                Order = 0,
                Title = "Pages",
                Content = "<i class='icon-dashboard'></i> Pages",
                Url = "/pages",
                TemplateUrl = "app/page/pages.html",
                MenuContainer = sideNav
            };
            var users = new MenuItem
            {
                Order = 1,
                Title = "Users",
                Content = "<i class='icon-dashboard'></i> Users",
                Url = "/users",
                TemplateUrl = "app/user/users.html",
                MenuContainer = sideNav
            };
            context.MenuItems.AddOrUpdate(h => h.Title, pages);
            context.MenuItems.AddOrUpdate(h => h.Title, users);
        }
    }
}
